from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import PostArticle
import markdown
from .forms import PostArticleForm
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.db.models import Q
from comment.models import Comment
from .models import ArticleColumn
from comment.forms import CommentForm

# Create your views here.

def article_list(request):
    search = request.GET.get('search')
    order = request.GET.get('order')
    column = request.GET.get('column')
    tag = request.GET.get('tag')
    article_list = PostArticle.objects.all()
    if search:
        article_list = article_list.filter(Q(title__icontains=search)
                                                |Q(body__icontains=search))
    else:
        search = ''

    if column is not None and column.isdigit():
        article_list = article_list.filter(column=column)

    if tag and tag != 'None':
        article_list = article_list.filter(tags__name__in=[tag])

    if order == 'total_views':
        article_list = article_list.order_by('-total_views')


    # if request.GET.get('order') == 'total_views':
    #     article_list = PostArticle.objects.all().order_by('-total_views')
    #     order = 'total_views'
    # else:
    #     article_list = PostArticle.objects.all()
    #     order = 'normal'

    paginator = Paginator(article_list,3)
    page = request.GET.get('page')
    articles = paginator.get_page(page)
    context = {'articles':articles,
               'order':order,
               'search':search,
               'column':column,
               'tag':tag,}

    return render(request, 'article/list.html', context)

def article_detail(request, id):
    article = PostArticle.objects.get(id=id)
    comments = Comment.objects.filter(article=id)
    article.total_views += 1
    article.save(update_fields=['total_views'])
    md = markdown.Markdown(extensions=[
                             'markdown.extensions.extra',
                             'markdown.extensions.codehilite',
                             'markdown.extensions.toc',
                         ])
    article.body = md.convert(article.body)
    comment_form = CommentForm()
    context = {'article':article,
               'toc':md.toc,
               'comments':comments,
               'comment_form':comment_form,}

    return render(request, 'article/detail.html', context)

@login_required(login_url='/userprofile/login/')
def article_create(request):

    if request.method == "POST":
        article_post_form = PostArticleForm(request.POST, request.FILES)
        if article_post_form.is_valid():

            new_article = article_post_form.save(commit=False)
            new_article.author = User.objects.get(id=request.user.id)
            if request.POST['column'] != 'none':
                new_article.column = ArticleColumn.objects.get(id=request.POST['column'])
            new_article.save()

            article_post_form.save_m2m()

            return redirect('article:article_list')
        else:
            return HttpResponse("表单内容有误，请重新填写。")
    else:
        article_post_form = PostArticleForm()
        columns = ArticleColumn.objects.all()
        context = {'article_post_form': article_post_form, 'columns':columns}

        return render(request, 'article/create.html', context)

@login_required(login_url='/userprofile/login/')
def article_delete(request, id):
    article = PostArticle.objects.get(id=id)
    if request.user != article.author:
        return HttpResponse("抱歉，你无权修改这篇文章。")
    article.delete()

    return redirect('article:article_list')

@login_required(login_url='/userprofile/login/')
def article_safe_delete(request, id):

    if request.method == 'POST':
        article = PostArticle.objects.get(id=id)
        if request.user != article.author:
            return HttpResponse("抱歉，你无权修改这篇文章")
        article.delete()

        return redirect('article:article_list')

    else:
        return HttpResponse("仅允许post请求")

@login_required(login_url='/userprofile/login/')
def article_update(request, id):
    article = PostArticle.objects.get(id=id)
    if request.user != article.author:
        return HttpResponse("抱歉，你无权修改这篇文章。")
    if request.method == 'POST':
        article_post_form = PostArticleForm(data=request.POST)
        if article_post_form.is_valid():
            article.title = request.POST['title']
            article.body = request.POST['body']
            if request.POST['column'] != 'none':
                article.column = ArticleColumn.objects.get(id=request.POST['column'])
            else:
                article.column = None
            if request.FILES.get('avatar'):
                article.avatar = request.FILES.get('avatar')
            article.tags.set(*request.POST.get('tags').split(','), clear=True)
            article.save()

            return redirect('article:article_detail', id=id)

        else:
            return HttpResponse("表单内容有误，请重新填写")

    else:
        article_post_form = PostArticleForm()
        columns = ArticleColumn.objects.all()
        context = {'article':article,
                   'article_post_form':article_post_form,
                   'columns':columns,
                   'tags':','.join([x for x in article.tags.names()])}

        return render(request, 'article/update.html', context)